package com.normal.zbase.impl.base

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.QuickViewHolder
import com.normal.zbase.BR

class BaseRecyclerViewAdapter<T : Any>(
    @LayoutRes var layoutResId: Int,

    ) :
    BaseQuickAdapter<T, QuickViewHolder>() {
    private var onBindViewHolder: OnBindViewHolder<T>? = null
    private var onItemChildClickCuListener: OnItemChildClickCuListener<T>? = null
    fun setOnBindViewHolder(onDataListener: OnBindViewHolder<T>?) {
        this.onBindViewHolder = onDataListener
    }

    fun addOnItemChildClickCuListener(
        @IdRes viewIds: MutableList<Int>,
        onItemChildClickCuListener: OnItemChildClickCuListener<T>?
    ) {
        this.onItemChildClickCuListener = onItemChildClickCuListener
        for (viewId in viewIds) {
            addOnItemChildClickListener(
                viewId
            ) { adapter, view, position ->
                onItemChildClickCuListener?.onItemClick(adapter, view, position)
            }
        }
    }


    override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: T?) {
        val dataBinding = DataBindingUtil.bind<ViewDataBinding>(holder.itemView)
        dataBinding!!.setVariable(BR.itemInfo, item)
        dataBinding.executePendingBindings()
        //二级内容扩展
        if (onBindViewHolder != null) {
            if (item != null) {
                onBindViewHolder!!.onResult(holder, position, item)
            }
        }
    }


    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int
    ): QuickViewHolder {

        return QuickViewHolder(layoutResId, parent)
    }


    interface OnBindViewHolder<T> {
        fun onResult(helper: QuickViewHolder?, position: Int, info: T)
    }

    fun interface OnItemChildClickCuListener<T> {
        fun onItemClick(adapter: BaseQuickAdapter<T, *>, view: View, position: Int)
    }
}